PreReq |
1. I know how to code in Java. 2. I know I need to think before I start coding. 3. I know basic OO concepts (inheritance, polyporphism, …). |
ObjTD |
Understand the importance of Design. |
Duration |
1 TD and 2 TPs (spread on 2 weeks) |
1. Classroom materials
| Do not hesitate to (re)read regularly the Course Material. |
2. The "SuperCanard" application
| This TD exercice is inspired from the excellent book "Head First: Design Pattern". Bert Bates, Eric Freeman, Elisabeth Freeman, Kathy Sierra. Editions O’Reilly. 2005. |
2.1. Existing application
You are asked to work on an existing app SuperCanard (duck, called canard in French, simulation game) which model (sorry for the French) is provided in the following class diagram:
Some other classes inherit from Canard.
|
Here is a code example:
Canard.javaabstract public class Canard {
public void cancaner() {
System.out.println("Je cancane comme un Canard!");
}
public void nager() {
System.out.println("Je nage comme un Canard!");
}
abstract public void afficher();
}
Colvert.javapublic class Colvert extends Canard {
public void afficher() {
System.out.println("Je suis un Colvert");
}
}
2.2. Modification/Improvement
Your boss requires that you upgrade the application in order to be a little more realistic.
You decide to add a voler() method to all your ducks:
Canard.javaabstract public class Canard {
public void cancaner() {
System.out.println("Je cancane comme un Canard!");
}
public void nager() {
System.out.println("Je nage comme un Canard!");
}
abstract public void afficher();
public void voler() {
System.out.println("Je vole comme un Canard!");
};
}
2.3. #WTF!
You receive an emergency call from your boss: in the application some plastic ducks start to fly!!! In addition, sick ducks, that shouldn’t fly, do so!
| You forgot that some kind of ducks do not fly! |
|
QUESTION
Complete this sentence: Inheritance is great to do …………. but is more problematic in terms of …………. |
2.4. Solution 1: redefine the methods
The first solution that comes to your mind is simple: redefine the voler() method for the ducks who don’t fly.
|
QUESTION
Complete the following java code to implement this solution:
|
|
QUESTION
In the following list, what are the problems that inheritance can raise to define the behavior of a
|
2.5. Solution 2: use of interfaces
You know try the use of interfaces to improve the code.
|
QUESTION
|
2.6. Solution 3: isolate what change
You realize you’re facing the kind of problem you had in the MPA module: CHANGES!
Let us then apply our first good principle :
|
Good design principle
Identify aspects of your code that vary and separate them from the one that don’t. |
|
QUESTION
What are the two main things that vary in your code? |
2.6.1. Implementing behaviors
Let’s try to implement behavior differently, so that they are separated from the rest of the code. For that we will use another good principle:
|
Good design principle
Program an interface, not an implementation. |
|
QUESTION
Propose a design (class diagram only)
with the following classes and/or interfaces (you’ll have to decide): |
2.6.2. Adding the new behaviors to the code
We have now to somehow link the behaviors to their corresponding ducks' class.
|
QUESTION
|
2.6.3. Summary and discussions
Let us now have a look at the overall design we have obtained.
|
QUESTION
|
2.7. Your first Design Pattern
2.7.1. The Strategy pattern
In fact you have just implemented your first Design Pattern : the Strategy pattern (Stratégie), sorry for the French:
|
Design pattern: Stratégie (Strategy)
Stratégie définit une famille d’algorithmes, encapsule chacun d’eux et les rend interchangeables. Il permet à l’algorithme de varier indépendamment des clients qui l’utilisent. Figure 3. Modèle UML du patron Strategy
|
2.7.2. Let’s try it on another application
|
You are asked to rework on an application where only the following model was produced (sorry again for the damn French):
|
Still hungry?
We have used without mentioning it a 3rd good principle:
|
Good design principle
Prefer composition than inheritance. |
|
QUESTION
What difference is there between our final design and this kind of implementation?:
|
| This crossword is taken from the book mentioned earlier, and hence includes definitions of words that you can’t guess: |
-
7 ⇒ Baleares
-
11 ⇒ Observateur
-
12 ⇒ DK
|
QUESTION
How would you test the presence of a Strategy pattern in an implementation ? |
| Do not hesitate to have a look at this other role playing example, available here (p.116). |